package com.gzh.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
public class FileUtils {
    public static File byteToFile(String path , MultipartFile multipartFile){
        File localFile = new File(path);
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(localFile,true);
            outputStream.write(multipartFile.getBytes());
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("字节写入文件错误",e);
                }
            }
        }
        return localFile;
    }

    public static void deleteFile(String path){
        File file = new File(path);
        file.deleteOnExit();
    }
}
